<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/18
 * Time: 10:08
 */

namespace app\admin\controller;


use app\admin\model\User;
use think\Controller;
use think\Session;

class Index extends Controller
{
    public function index(){
        $account = $this->request->post('account');
        $password = $this->request->post('password');

        $code= $this->request->post('code');
        if(!(new \think\captcha\Captcha())->check($code)){
            $this->error("验证码错误");
        }

        $user=new User();
        $data=$user->where([
            "mobile"        =>$account,
            "scope"         =>1
        ])->find();

        if(!password_verify($password,$data->password)){
            $this->error("用户名或密码错误");
        }

        (new Session())->set("user",[
            "id"        =>$data->uid,
            "scope"     =>$data->scope,
            "nickname"  =>$data->nickname
        ]);
        //return $this->fetch("index");
        $this->redirect("main");
    }

    //子后台入口
    public function index1(){
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        $code= $this->request->post('code');

        if(!(new \think\captcha\Captcha())->check($code)){
            $this->error("验证码错误");
        }

        $user=new User();
        $data=$user->where([
            "mobile"        =>$account,
            "password"      =>$password,
            "scope"         =>0
        ])->find();

        if(!$data){
            $this->error("用户名或密码错误");
        }

        (new Session())->set("user",[
            "id"        =>$data->uid,
            "scope"     =>$data->scope,
            "nickname"  =>$data->nickname
        ]);
        //return $this->fetch("index1");
        $this->redirect("main1");
    }

    public function main(){
        return $this->fetch("index");
    }

    public function main1(){
        return $this->fetch("index1");
    }

}